<?php
/*
 * This file is part of Big Picture Gallery (BPG).
 *
 * Big Picture Gallery (BPG) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Big Picture Gallery (BPG) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Big Picture Gallery (BPG).  If not, see <http://www.gnu.org/licenses/>.
 */
class Router {
  /**
   * @the registry
   */
  private $registry;

  /**
   * @the controller path
   */
  private $path;

  private $args = array();

  private $file;

  private $controller;

  private $action;

  public function __construct($registry) {
    $this->registry = $registry;
    $this->setPath(_ROOT_ . '/app/controller');
  }

  /**
   * @set controller directory path
   *
   * @param string $path
   *
   * @return void
   */
  private function setPath($path) {
    /*** check if path is a directory ***/
    if (is_dir($path) == false) {
      throw new Exception ('Invalid controller path: `' . $path . '`');
    }
    /*** set the path ***/
    $this->path = $path;
  }

  /**
   * @load the controller
   *
   * @access public
   *
   * @return void
   */
  public function loader() {
    /*** check the route ***/
    $this->getController();

    /*** if the file is not there diaf ***/
    if (is_readable($this->file) == false) {
      $this->file = $this->path.'/error404controller.php';
      $this->controller = 'error404';
    }

    /*** a new controller class instance ***/
    $class = $this->controller . 'Controller';
    $controller = new $class($this->registry);

    /*** check if the action is callable ***/
    if (is_callable(array($controller, $this->action)) == false) {
      $action = 'index';
    }
    else {
      $action = $this->action;
    }
    /*** run the action ***/
    $controller->$action($this->value);
  }

  /**
   * @get the controller
   *
   * @access private
   *
   * @return void
   */
  private function getController() {

    /*** get the route from the url ***/
    $route = (empty($_GET['rt'])) ? '' : $_GET['rt'];

    if (empty($route)) {
      $route = 'index';
    }
    else {
      /*** get the parts of the route ***/
      $parts = explode('/', $route);
      $this->controller = $parts[0];
      if(isset( $parts[1])) {
        $this->action = $parts[1];
      }
      if(isset( $parts[2])) {
        $this->value = $parts[2];
      }
    }

    if (empty($this->controller)) {
      $this->controller = 'index';
    }

    /*** Get action ***/
    if (empty($this->action)) {
      $this->action = 'index';
    }

    if (empty($this->value)) {
      $this->value = '';
    }

    /*** set the file path ***/
    $this->file = $this->path .'/'. $this->controller . 'Controller.class.php';
  }
}

?>
